package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.User;

public class UserDAOImpl extends BaseHibernateDAOImpl<User> implements UserDAO {
	@SuppressWarnings("unchecked")
	public List<User> getAllUser() {
		List<User> users;
		try {
			Query query = getSession().getNamedQuery("AllUserQuery");
			users = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return users;
	}

	public User getUserByUserID(String userID) {
		User user;
		try {
			user = (User) getSession().get(User.class, userID);
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return user;
	}

	public User getUserByUserName(String userName) {
		User user;
		try {
			Query query = getSession().getNamedQuery("UserByUserNameQuery");
			query.setString("userName", userName);
			user = (User) query.uniqueResult();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return user;
	}

	public User login(String loginName, String userPassword) {
		User user;
		try {
			Query query = getSession().getNamedQuery("UserByLoginQuery");
			query.setString("loginName", loginName);
			query.setString("userPassword", userPassword);
			user = (User) query.uniqueResult();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return user;
	}

}
